package cn.com.header.zbpk.account.service.impl;

import cn.com.header.core.util.SysProperties;
import cn.com.header.zbpk.account.entity.Role;
import cn.com.header.zbpk.account.entity.RoleAuthority;
import cn.com.header.zbpk.account.mapper.IRoleAuthorityMapper;
import cn.com.header.zbpk.account.model.RoleType;
import cn.com.header.zbpk.account.service.IRoleAuthorityService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Yuanyifeng
 * @date 2017-11-20
 * @time 14:02
 */
@Service
public class RoleAuthorityServiceImpl implements IRoleAuthorityService {
    @Autowired
    private IRoleAuthorityMapper iRoleAuthorityMapper;

    @Override
    public void save(RoleAuthority roleAuthority) {
        iRoleAuthorityMapper.insert(roleAuthority);
    }

    /**
     * 新增角色时用到，产生角色资源数据
     * @param role
     */
    @Override
    public void genearteRoleAuthority(Role role) {
        List<String> authorityList = Lists.newArrayList();
        if (role.getType().equals(RoleType.TEACHER)) {//如果是教师
            authorityList = Lists.newArrayList(SysProperties.TEACHER_AUTHORITYIDS);
        } else if (role.getType().equals(RoleType.STUDENT)) {//如果是学生
            authorityList = Lists.newArrayList(SysProperties.STUDENT_AUTHORITYIDS);
        } else if (role.getType().equals(RoleType.SCHOOL_MASTER)) {//如果是校长
            authorityList = Lists.newArrayList(SysProperties.SCHOOL_MASTER_AUTHORITYIDS);
        } else if (role.getType().equals(RoleType.SCHOOL_ADMIN)) {//如果是学校管理员
            authorityList = Lists.newArrayList(SysProperties.SCHOOL_ADMIN_AUTHORITYIDS);
        }
        if (!authorityList.isEmpty()) {
            //插入角色权限关联数据
            for (String authId : authorityList) {
                RoleAuthority roleAuthority = new RoleAuthority();
                roleAuthority.setAuthorityId(authId);
                roleAuthority.setRoleId(role.getId());
                iRoleAuthorityMapper.insert(roleAuthority);
            }
        }
    }
}
